<?php

include_once  'GanJiFunc.php';
include 'GanJiRelationFunc.php';

function mb_unserialize($serial_str) {
    $out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
    return unserialize($out);
}
function loadJaChiData($targetYear)
{
    $fn='../YellowCal/jachis/'.$targetYear.'.ser';
    $streamed=file_get_contents($fn);
    $data=mb_unserialize($streamed);
    return $data;
}
function  forwardSeekJaChi($tt,$targetYear)
{
    $data=loadJaChiData($targetYear);
    $sz=count($data);
    for($i=0;$i<$sz;$i++)
    {
        if($data[$i]['type']!='節')
            continue;
        $tchi=strtotime($data[$i]['time']);
        if($tchi>=$tt)
        {
            $diff=$tchi-$tt;
            $result=array();
            $result['diff']=$diff;
            $result['name']=$data[$i]['name'];
            $result['time']=$data[$i]['time'];
            $dt=$tchi-$tt; //差多少秒
            $dt=$dt/(60*60*24*3);//乃是 ..3天為1年, 30天=10年
            $subz=(int)(($dt-(int)$dt)*12+1);
            $result['start']=(int)$dt+1;
            $result['起運']=$result['start'].'歲'.$subz.'月';
            var_dump($result);
            return $result;
        }
    }
    return null;

}
function backwardSeekJaChi($tt,$targetYear)
{
    $data=loadJaChiData($targetYear);
    $sz=count($data);
    for($i=$sz-1;$i>=0;$i--)
    {
        if($data[$i]['type']!='節')
            continue;
        $tchi=strtotime($data[$i]['time']);
        if($tchi<=$tt)
        {
            $diff=$tt-$tchi;
            $result=array();
            $result['diff']=$diff;
            $result['name']=$data[$i]['name'];
            $result['time']=$data[$i]['time'];
            $dt=$tt-$tchi; //差多少秒
            $dt=$dt/(60*60*24*3);//乃是 ..3天為1年, 30天=10年
            $subz=(int)(($dt-(int)$dt)*12+1);
            $result['start']=(int)$dt+1;
            $result['起運']=$result['start'].'歲'.$subz.'月';
            var_dump($result);
            return $result;
        }
    }
    return null;
}
function  backwardSeek($y,$m,$d,$h=0,$i=0,$targetYear)
{
    $ss=$y.'-'.$m.'-'.$d.'-'.$h.'-'.$i;
    $tt=strtotime($ss);
    $result=backwardSeekJaChi($tt,$targetYear+1);
    if($result==null)
        $result=backwardSeekJaChi($tt,$targetYear);
    if($result==null)
        $result=backwardSeekJaChi($tt,$targetYear-1);
    return $result;
}
function  forwardSeek($y,$m,$d,$h=0,$i=0,$targetYear)
{
    $ss=$y.'-'.$m.'-'.$d.'-'.$h.'-'.$i;
    $tt=strtotime($ss);
    $result=forwardSeekJaChi($tt,$targetYear-1);
    if($result==null)
        $result=forwardSeekJaChi($tt,$targetYear);
    if($result==null)
        $result=forwardSeekJaChi($tt,$targetYear+1);
    return $result;     
}

function genTenyearFortunes($startYear,$Gan,$Ji,$clockwise)
{
    $result=array();
    for($i=0;$i<9;$i++)
    {
        $y=$startYear+$i*10;
        if($clockwise)
        {
            $Gan=GanJiFunc::nextGan($Gan);
            $Ji=GanJiFunc::nextJi($Ji);
        }else
        {
            $Gan=GanJiFunc::prevGan($Gan);
            $Ji=GanJiFunc::prevJi($Ji);
        }
        $arr=array();
        $arr['age']=$y;
        $arr['大運']=$Gan.$Ji;
        $result[]=$arr;
    }   
    return $result;   
}
function calTenyearFortunes($sex,$y,$m,$d,$h=0,$i=0)
{
    $result=GanJiFunc::generateEightWord($y,$m,$d,$h,$i);
    $isYan=GanJiFunc::isYanGan($result['年干支']['干']);

    $clockwise=true;

    if($sex=='M')
        $clockwise=$isYan;
    else
        $clockwise=!$isYan;
    
    if($clockwise)
    {
     $startSeg=forwardSeek($y, $m, $d,$h,$i,$y);
    }   else
    {
     $startSeg=backwardSeek($y, $m, $d,$h,$i,$y);
     //Gen
    }
    //Gen
    $rr=genTenyearFortunes($startSeg['start'],$result['月干支']['干'],$result['月干支']['支'], $clockwise);
    foreach($rr as $r)
    {
        echo $r['age'].'    '.$r['大運'].PHP_EOL;
    }

}

/*
$t1=strtotime('2012-12-21 01:01:01');
$t2=strtotime('2012-12-21 01:01:01  +1 day');

echo date('Y-n-j',$t2).PHP_EOL;
echo $t2-$t1;                
*/

//calTenyearFortunes('M',1975,4,28,2,0);

$rr=GanJiFunc::generateEightWord(2012,12,20);
$d=$rr['日干支']['干支'];

echo '本日'.$d.PHP_EOL;

$g= new GanJiRelationFunc();

$result=$g->getRelation("甲辰",$d);
var_dump($result);
$result=$g->getRelation("甲辰",$rr['年干支']['干支']);
var_dump($result);
$result=$g->getRelation("甲辰",$rr['月干支']['干支']);
var_dump($result);


/*$result=array();
GanJiRelationFunc::check進錄($s1,$s3,$result);
var_dump($result);
*/


//$g->getRelation('甲子','乙卯');

